<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { loveMessages, colorThemes } from './data/loveMessages'

const message = ref('')
const colorTheme = ref({ bg: '#FF6B9D', text: '#FFFFFF' })

onMounted(async () => {
  // 方法1: 监听 IPC 消息（主进程发送）
  if (window.electron?.ipcRenderer) {
    window.electron.ipcRenderer.on('window-config', (_event, config: { messageIndex: number; colorIndex: number }) => {
      message.value = loveMessages[config.messageIndex] || loveMessages[0]
      colorTheme.value = colorThemes[config.colorIndex] || colorThemes[0]
    })
  }
  
  // 方法2: 主动请求配置（作为备用方案）
  if (window.api?.getWindowConfig) {
    try {
      const config = await window.api.getWindowConfig()
      if (config && config.messageIndex !== undefined && config.colorIndex !== undefined) {
        message.value = loveMessages[config.messageIndex] || loveMessages[0]
        colorTheme.value = colorThemes[config.colorIndex] || colorThemes[0]
      }
    } catch (error) {
      console.error('Failed to get window config:', error)
      // 如果 IPC 失败，使用随机值
      const randomMessageIndex = Math.floor(Math.random() * loveMessages.length)
      const randomColorIndex = Math.floor(Math.random() * colorThemes.length)
      message.value = loveMessages[randomMessageIndex]
      colorTheme.value = colorThemes[randomColorIndex]
    }
  } else {
    // 如果都不支持，使用 URL 参数作为最后的后备
    const urlParams = new URLSearchParams(window.location.search)
    const messageIndex = parseInt(urlParams.get('messageIndex') || '0', 10)
    const colorIndex = parseInt(urlParams.get('colorIndex') || '0', 10)
    message.value = loveMessages[messageIndex] || loveMessages[0]
    colorTheme.value = colorThemes[colorIndex] || colorThemes[0]
  }
})
</script>

<template>
  <div 
    class="love-window"
    :style="{
      background: colorTheme.bg,
      color: colorTheme.text
    }"
  >
    <div class="content">
      <p class="message">{{ message }}</p>
    </div>
  </div>
</template>

<style scoped>
.love-window {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  box-shadow: none;
  overflow: hidden;
  cursor: default;
  user-select: none;
  transition: transform 0.2s ease;
  box-sizing: border-box;
}

.love-window:hover {
  transform: scale(1.02);
}

.content {
  padding: 16px 20px;
  text-align: center;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.message {
  font-size: 16px;
  font-weight: 500;
  line-height: 1.5;
  margin: 0;
  word-wrap: break-word;
  word-break: break-word;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  line-clamp: 4;
  -webkit-box-orient: vertical;
}
</style>
